package com.zzjson.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.thread</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年09月02日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */

public class LockOrdering {
	private Lock lock1 = new ReentrantLock();
	private Lock lock2 = new ReentrantLock();

	public static void main(String... args) {
		final LockOrdering test = new LockOrdering();
		new Thread() {
			@Override
			public void run() {
				test.op1();
			}
		}.start();
		new Thread() {
			@Override
			public void run() {
				test.op2();
			}
		}.start();
	}

	public void op1() {
		lock1.lock();
		System.out.println("------o1----------");
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (Exception e) {
		}

		lock2.lock();
		System.out.println("------o1----oo------");
		lock2.unlock();
		System.out.println("--------01111");
		lock1.unlock();
	}

	public void op2() {
		//锁获取的顺序与op1相反
		lock2.lock();
		System.out.println("------o2----------");
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (Exception e) {
		}

		lock1.lock();
		System.out.println("------o2----oo------");
		lock1.unlock();
		lock2.unlock();
	}
}